# This file is part of the Net::Tube module.
#
# Net::Tube is Free Software; you can use, redistribute it and/or
# modify it under the terms of either:
#
# a) the GNU General Public License as published by the Free Software
#    Foundation, either version 2, or (at your opinion) any later
#    version, or
#
# b) the "Artistic License" which comes with Perl.
#
use strict;
use warnings;
use Test::Simple tests => 4;

use Net::Tube;
use Net::Tube::Loader::File;
use Net::Tube::Filter::SimpleMerge;

sub test_new {
    my $x = Net::Tube::Filter::SimpleMerge->new();
    unless ( defined $x && ref $x ) {
        print "#object not created \n";
        return 0;
    }
    unless ( $x->isa("Net::Tube::Filter::SimpleMerge") ) {
        print "# wrong object type created\n";
        return 0;
    }
    return 1;
}

ok( test_new(), "test new" );

sub test_merge {
    my $m = Net::Tube::Machine->new();
    my $c =
      Net::Tube::Loader::File->new(
        { loadSource => 'tdata/local_config.xml' } );
    my $f  = Net::Tube::Loader::File->new( { loadXPath => '/config/file' } );
    my $sm = Net::Tube::Filter::SimpleMerge->new;

    $sm->set_config_entry( 'mergerElement', { name => 'simple' } );

    my $tv =
"<?xml version=\"1.0\"?>\n<simple><A>foo</A><B>foo</B></simple>\n";
    my $data;
    eval { $data = $m->execute( [ $c, $f, $sm ] ); };
    if ($@) {
        print "# internal error: $@\n";
        return 0;
    }
    unless ( defined $data && length $data ) {
        print "tube machine generated no output\n";
        return 0;
    }
    unless ( $tv eq $data ) {
        print "# tube returned an unexpected result\n"
          . " received '$data'\n"
          . "expected '$tv'\n";    
        return 0;
    }
    return 1;
}

ok( test_merge(), "merge" );

sub test_merge_ns_noprefix {
    my $m = Net::Tube::Machine->new();
    my $c =
      Net::Tube::Loader::File->new(
        { loadSource => 'tdata/local_config.xml' } );
    my $f  = Net::Tube::Loader::File->new( { loadXPath => '/config/file' } );
    my $sm = Net::Tube::Filter::SimpleMerge->new;

    $sm->set_config_entry( 'mergerElement', { name => 'simple',
            'namespace-uri' => 'http://lo-f.at' } );

    my $tv =
"<?xml version=\"1.0\"?>\n<simple xmlns=\"http://lo-f.at\"><A>foo</A><B>foo</B></simple>\n";
    my $data;
    eval { $data = $m->execute( [ $c, $f, $sm ] ); };
    if ($@) {
        print "# internal error: $@\n";
        return 0;
    }
    unless ( defined $data && length $data ) {
        print "tube machine generated no output\n";
        return 0;
    }
    unless ( $tv eq $data ) {
        print "# tube returned an unexpected result\n"
          . " received '$data'\n"
          . "expected '$tv'\n";    
        return 0;
    }
    return 1;
}

ok( test_merge_ns_noprefix(), "merge ns" );

sub test_merge_ns_prefix {
    my $m = Net::Tube::Machine->new();
    my $c =
      Net::Tube::Loader::File->new(
        { loadSource => 'tdata/local_config.xml' } );
    my $f  = Net::Tube::Loader::File->new( { loadXPath => '/config/file' } );
    my $sm = Net::Tube::Filter::SimpleMerge->new;

    $sm->set_config_entry( 'mergerElement', { name => 'simple',
            'namespace-uri' => 'http://lo-f.at',
            'namespace-prefix'=>'lofat' } );

    my $tv =
"<?xml version=\"1.0\"?>\n<lofat:simple xmlns:lofat=\"http://lo-f.at\"><A>foo</A><B>foo</B></lofat:simple>\n";
    my $data;
    eval { $data = $m->execute( [ $c, $f, $sm ] ); };
    if ($@) {
        print "# internal error: $@\n";
        return 0;
    }
    unless ( defined $data && length $data ) {
        print "tube machine generated no output\n";
        return 0;
    }
    unless ( $tv eq $data ) {
        print "# tube returned an unexpected result\n"
          . " received '$data'\n"
          . "expected '$tv'\n";    
        return 0;
    }
    return 1;
}

ok( test_merge_ns_prefix(), "merge ns prefix" );

